<?php

/**
 * Pwiff - PHP SWF Library
 *
 * LICENSE
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 *
 * @package    Pwiff
 * @subpackage Records
 * @copyright  Copyright (c) 2010 Steven Harris
 * @license    http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @version    $Id: Header.php 5 2010-02-14 05:08:39Z indigo.sunrise $
 */

/**
 * The SWF Header.
 *
 * @package    Pwiff
 * @subpackage Records
 */
class Pwiff_Record_Header extends Pwiff_Record
{
	/**
	 * Compression flag.
	 *
	 * @var bool
	 */
	protected $compressed = false;
	
	/**
	 * SWF file version.
	 *
	 * @var int
	 */
	protected $version = 10;
	
	/**
	 * Length of the file in bytes.
	 *
	 * @var int
	 */
	protected $fileLength = 0;
	
	/**
	 * Frame size in twips.
	 *
	 * @var Pwiff_Record_Rect
	 */
	protected $frameSize = null;
	
	/**
	 * Frame rate.
	 *
	 * @var float
	 */
	protected $frameRate = 0;
	
	/**
	 * Total number of frames in the file.
	 * 
	 * @var int
	 */
	protected $frameCount = 0;
	
	/**
	 * Sets whether the file is compressed.
	 *
	 * @param  bool $compressed
	 * @return Pwiff_Record_Header
	 */
	public function setCompressed($compressed)
	{
		$this->compressed = $compressed;
		return $this;
	}
	
	/**
	 * Sets the SWF version of the file.
	 * 
	 * @param  int $version
	 * @return Pwiff_Record_Header
	 */
	public function setVersion($version)
	{
		$this->version = $version;
		return $this;
	}
	
	/**
	 * Sets the length of the SWF file.
	 *
	 * @param  int $fileLength
	 * @return Pwiff_Record_Header
	 */
	public function setFileLength($fileLength)
	{
		$this->fileLength = $fileLength;
		return $this;
	}
	
	/**
	 * Sets the frame size.
	 *
	 * @param  Pwiff_Record_Rect $frameSize
	 * @return Pwiff_Record_Header
	 */
	public function setFrameSize($frameSize)
	{
		$this->frameSize = $frameSize;
		return $this;
	}
	
	/**
	 * Sets the frame size using width and height, in twips.
	 *
	 * @param  int $width
	 * @param  int $height
	 * @return Pwiff_Record_Header
	 */
	public function setFrameSizeXY($width, $height)
	{
		$this->frameSize = new Pwiff_Record_Rect(0, $width, 0, $height);
	}
	
	/**
	 * Sets the frame rate.
	 *
	 * @param  float $frameRate
	 * @return Pwiff_Record_Header
	 */
	public function setFrameRate($frameRate)
	{
		$this->frameRate = $frameRate;
		return $this;
	}
	
	/**
	 * Sets the number of frames in the file.
	 *
	 * @param  int $frameCount
	 * @return Pwiff_Record_Header
	 */
	public function setFrameCount($frameCount)
	{
		$this->frameCount = $frameCount;
		return $this;
	}
	
	/**
	 * Returns whether the SWF file will be compressed.
	 *
	 * @return bool
	 */
	public function isCompressed()
	{
		return $this->compressed;
	}
	
	/**
	 * Gets the SWF file version.
	 * 
	 * @return int
	 */
	public function getVersion()
	{
		return $this->version;
	}
	
	/**
	 * Gets the file length.
	 *
	 * @return int
	 */
	public function getFileLength()
	{
		return $this->fileLength;
	}
	
	/**
	 * Gets the frame size as a Rect record.
	 *
	 * @return Pwiff_Record_Rect
	 */
	public function getFrameSize()
	{
		return $this->frameSize;
	}
	
	/**
	 * Gets the frame size as array(width, height). If the frame size has
	 * not been set, this returns array(0, 0).
	 *
	 * @return array
	 */
	public function getFrameSizeXY()
	{
		if ($this->frameSize === null) {
			return array(0, 0);
		}
		return array($this->frameSize->getMaxX(), $this->frameSize->getMaxY());
	}
	
	/**
	 * Gets the frame rate.
	 *
	 * @return float
	 */
	public function getFrameRate()
	{
		return $this->frameRate;
	}
	
	/**
	 * Gets the number of frames in the file.
	 *
	 * @return int
	 */
	public function getFrameCount()
	{
		return $this->frameCount;
	}
	
	/**
	 * Gets the record length.
	 *
	 * @return int
	 */
	public function getRecordLength()
	{
		// signature + version + file length + frame rate + frame count
		// = 3 + 1 + 4 + 2 + 2
		$length = 12;
		
		// add frame size rect
		$length += $this->frameSize->getRecordLength();
		
		return $length;
	}
	
	/**
	 * Writes the SWF header to the given output instance.
	 *
	 * @param  Pwiff_Output $out
	 */
	public function write(Pwiff_Output $out)
	{
		// write the signature
		$out->writeChar($this->compressed ? 'C' : 'F')
		    ->writeChar('W')
		    ->writeChar('S');
		
		// version, file length
		$out->writeUI8($this->version)
		    ->writeUI32($this->fileLength);
		
		// frame size
		$this->frameSize->write($out);
		
		// frame rate, frame count
		$out->writeFixed8($this->frameRate)
		    ->writeUI16($this->frameCount);
	}
}
